package com.gtw.modules.sys.controller;

import com.gtw.common.annotation.LoginUserApi;
import com.gtw.common.utils.PageUtils;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.modules.api.service.TbAddressService;
import com.gtw.modules.api.service.TbShoppingCartServices;
import com.gtw.modules.api.service.TbUserService;
import com.gtw.modules.base.service.TbCityService;
import com.gtw.modules.base.service.TbDistrictService;
import com.gtw.modules.base.service.TbProvinecService;
import com.gtw.modules.entity.*;
import com.gtw.modules.sys.service.TbOutGoosService;
import net.sf.json.JSONArray;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by wuye on 2018/3/26.
 */
@RestController
@RequestMapping("/sys/outgoos")
public class SysTbOutGoosController {

    @Autowired
    private TbOutGoosService outgoosService;

    @Autowired
    private TbAddressService tbAddressService;

    @Autowired
    private TbCityService tbCityService;

    @Autowired
    private TbProvinecService tbProvinecService;

    @Autowired
    private TbDistrictService tbDistrictService;

    @Autowired
    private TbShoppingCartServices tbShoppingCartServices;

    @Autowired
    private TbUserService tbUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:outgoos:list")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);

        List<TbOutGoos> outgoosList = outgoosService.queryList(query);
        int total = outgoosService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(outgoosList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:outgoos:info")
    public R info(@PathVariable("id") Integer id){
        TbOutGoos outgoos = outgoosService.queryObject(id);
        TbAddress tbaddress = tbAddressService.queryObject(Integer.valueOf(outgoos.getAddress()));
        String address = "";
        TbCity tbCity = tbCityService.findByCityId(tbaddress.getCityid());
        TbDistrict tbDistrict = tbDistrictService.findByDistrictId(tbaddress.getDistrictid());
        TbProvinec tbProvinec = tbProvinecService.queryObject(tbaddress.getProvinceid());
        address += tbProvinec.getProvincename()+" "+tbCity.getCityname()+" "+tbDistrict.getDistrictname()+" "+tbaddress.getDetails()+" "+"收货人:"+tbaddress.getUsername();
        String path = outgoos.getShoppingid();
        outgoos.setPostcode(tbaddress.getPostcode());
        outgoos.setPhone(tbaddress.getPhone());
        JSONArray ja = JSONArray.fromObject(path);
        List<TbShoppingCart> list = new ArrayList<>();
        for (int i = 0; i < ja.size(); i++) {
            TbShoppingCart tbShoppingCart = tbShoppingCartServices.selectByPrimaryKey(Integer.parseInt(ja.get(i).toString()));
            list.add(tbShoppingCart);
        }
        return R.ok().put("outgoos", outgoos).put("address",address).put("list",list);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:outgoos:save")
    public R save(@RequestBody TbOutGoos outgoos){
        outgoosService.save(outgoos);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:outgoos:update")
    public R update(@RequestBody TbOutGoos outgoos){
        outgoosService.update(outgoos);

        return R.ok();
    }


    /**
     * 发货
     */
    @RequestMapping("/fahuo")
    public R fahuo(@RequestBody TbOutGoos outgoos){
        TbOutGoos tboutgoos = outgoosService.queryObject(outgoos.getId());
        if(tboutgoos.getState() == 2){
            return R.error().put("msg","已经发货了");
        }
        if(tboutgoos.getState() == 3){
            return R.error().put("msg","已经发货了");
        }
        tboutgoos.setWuliutype(outgoos.getWuliutype());
        tboutgoos.setWuliuorderno(outgoos.getWuliuorderno());
        tboutgoos.setState(2);
        tboutgoos.setUpdatedate(new Date());
        outgoosService.update(tboutgoos);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("outgoos:delete")
    public R delete(@RequestBody Integer[] ids){
        outgoosService.deleteBatch(ids);
        return R.ok();
    }
}
